package com.zingrow.web.customer.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zingrow.common.utils.PageView;
import com.zingrow.web.customer.mapper.GroupMapper;
import com.zingrow.web.customer.mapper.GroupSchemeMapper;
import com.zingrow.web.customer.model.Group;
import com.zingrow.web.customer.service.IGroupService;
import com.zingrow.web.report.mapper.RptModelMapper;
import com.zingrow.web.report.mapper.RptModuleInfoMapper;
import com.zingrow.web.report.model.RptModel;
import com.zingrow.web.user.mapper.UserMapper;
import com.zingrow.web.user.mapper.UserModelSchemeMapper;
import com.zingrow.web.user.mapper.UserPermissionMapper;
import com.zingrow.web.user.model.User;

@Service
public class GroupService implements IGroupService {

	@Autowired
	private GroupMapper groupMapper;
	@Autowired
	private GroupSchemeMapper groupSchemeMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserPermissionMapper userPermissionMapper;
	@Autowired
	private UserModelSchemeMapper userModelSchemeMapper;
	@Autowired
	private RptModelMapper rptModelMapper;
	@Autowired
	private RptModuleInfoMapper rptModuleInfoMapper;

	@Override
	public List<Group> select(String gName, String gBrie, PageView pageView) {
		Map<String, Object> map = new HashMap<>();
		// 添加分页参数
		map.put("paging", pageView);
		// 添加所需要查询的参数
		if (StringUtils.isNotBlank(gName)) {
			gName = "%" + gName + "%";
			map.put("groupName", gName);
		}
		if (StringUtils.isNotBlank(gBrie)) {
			gBrie = "%" + gBrie + "%";
			map.put("groupBrie", gBrie);
		}

		List<Group> list = groupMapper.select(map);

		return list;
	}
	/**
	 * 新增
	 */
	@Override
	public boolean addGroup(Group group) {
		//默认当前时间
		if (null==group.getInsertTime()) {
			group.setInsertTime(new Date());
		}
		//默认状态可用1
		if (null==group.getIsDelete()) {
			group.setIsDelete(1);
		}
		//默认没有权限0
		if (null==group.getRptRight()) {
			group.setRptRight(0);
		}
		return groupMapper.insert(group) > 0;
	}

	/**
	 * 删除
	 */
	@Override
	@Transactional
	public boolean delGroup(Integer groupId) {
		/*Map<String,Object> map = new HashMap<String, Object>();
		map.put("groupId", groupId);*/
		int i = groupMapper.deleteByPrimaryKey(groupId);// 删除组织 -更改状态
		if (i > 0) {
			List<RptModel> listmodel = rptModelMapper.selectBygroupId(groupId); //根据组织ID查询报告模板
			if (listmodel.size()>0) {
				for (RptModel rptModel : listmodel) {
					rptModuleInfoMapper.deleteByPrimaryKey(rptModel.getId());//根据模板ID删除模块数据
				}
			}
			List<User> list = userMapper.selectId(groupId);  //根据组织ID查询组织下的所有用户
			if (list.size()>0) {
				//循环读取用户ID
				for (User user : list) {
					userModelSchemeMapper.delete(user.getUserId()); //删除用户报告权限数据
				}
			}
			rptModelMapper.deleteByGroupId(groupId);  //根据组织ID删除报告模板数据
			groupSchemeMapper.delGroupScheme(groupId);// 删除组织方案
			userMapper.delGroupUser(groupId);// 删除用户
			userPermissionMapper.delGroupUserByPermission(groupId);// 删除组织用户方案
			return true;
		}
		return false;
	}
	/**
	 * 修改
	 */
	@Override
	public boolean updateGroup(Group group) {
		if (null==group.getUpdateTime()) {
			group.setUpdateTime(new Date());
		}
		return groupMapper.updateByPrimaryKey(group) > 0;
	}

	/**
	 * 同名查询
	 */
	@Override
	public boolean querySameByGroup(String gName, Integer gId) {
		return groupMapper.selectSameByGroup(gName, gId) > 0;
	}

	@Override
	public List<Group> selectAllName(String gName) {

		if (null != gName && !"".equals(gName)) {
			gName = "%" + gName + "%";
		}
		return groupMapper.selectAllName(gName);
	}
	@Override
	public List<Group> selectfidName(String ifmId){
		return groupMapper.selectfidName(ifmId);
	}
	/**
	 * 查询所有组织
	 */
	@Override
	public List<Group> selectAll() {
		return groupMapper.selectAll();
	}
	/**
     * 根据组织id查询下面所有的方案id
     */
	@Override
	public List<Group> selectgroupid(Integer groupId) {
		return groupMapper.selectgroupid(groupId);
	}
	
	@Override
	public String selectGroupBrieByGroupid(Integer groupId) {
		return groupMapper.selectGroupBrieByGroupid(groupId);
	}
	@Override
	public String selectGroupNameByGroupid(Integer groupId) {
		return groupMapper.selectGroupNameByGroupid(groupId);
	}
	
	/**
	 * 查询该组织栏目字段具体数值 
	 * @param groupId
	 * @return
	 */
	@Override
	public Group selectpossess(Integer groupId) {
		return groupMapper.selectpossess(groupId);
	}
	/**
	 * 判断查询组织可拥有用户数 
	 * @param groupId
	 * @return
	 */
	@Override
	public int selectUsernum(Integer groupId) {
		return groupMapper.selectUsernum(groupId);
	}
	@Override
	public Group selectgroupName(Integer groupId) {
		return groupMapper.selectgroupName(groupId);
	}
	//根据组织ID修改组织报告权限
	@Override
	public boolean updateByGroupId(Group group) {
		int count = groupMapper.updateByGroupId(group);
		if (group.getRptRight()==0) {
			// 根据组织id查询该组织下的所有用户
			List<User> list = userMapper.selectId(group.getGroupId()); // 根据组织ID查询组织下的所有用户
			if (list.size() > 0) {
				// 循环读取用户ID
				for (User user : list) {
					userModelSchemeMapper.delete(user.getUserId()); // 删除用户报告权限数据
				}
			}
		}
		return count>0;
	}
	@Override
	public Group selectgruopgroupid(Integer groupId){
		return groupMapper.selectgruopgroupid(groupId);
	};
}
